package com.ruoyi.web.controller.inspection;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.config.HeadNum;
import com.ruoyi.common.utils.BeanUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.GetSessionValueUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.inspection.domain.FzInspectionDian;
import com.ruoyi.inspection.domain.FzInspectionRouteInfo;
import com.ruoyi.inspection.service.IFzInspectionDianService;
import com.ruoyi.inspection.service.IFzInspectionRouteInfoService;
import com.ruoyi.web.controller.inspection.vo.FzInspectionRouteInfoVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static java.util.stream.Collectors.groupingBy;

/**
 * 路线信息Controller
 * 
 * @author ruoyi
 * @date 2020-03-13
 */
@Controller
@RequestMapping("/inspection/info")
public class FzInspectionRouteInfoController extends BaseController
{
    private String prefix = "inspection/info";

    @Autowired
    private IFzInspectionRouteInfoService fzInspectionRouteInfoService;
    @Autowired
    private IFzInspectionDianService fzInspectionDianService;

    @RequiresPermissions("inspection:info:view")
    @GetMapping()
    public String info(ModelMap map, @RequestParam("routeId")String routeId)
    {
        map.put("routeId",routeId);
        return prefix + "/info";
    }

    /**
     * 查询路线信息列表
     */
    @RequiresPermissions("inspection:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FzInspectionRouteInfo fzInspectionRouteInfo,@RequestParam("routeId")String routeId)
    {
        startPage();
        fzInspectionRouteInfo.setRouteId(routeId);
        List<FzInspectionRouteInfo> list = fzInspectionRouteInfoService.selectFzInspectionRouteInfoList(fzInspectionRouteInfo);
        List<FzInspectionRouteInfoVo> fzInspectionRouteInfoVos=new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)) {
            List<String> inspectionRouteInfoId = list.stream().map(FzInspectionRouteInfo::getInspectionId).collect(Collectors.toList());
            List<FzInspectionDian> fzInspectionDians = fzInspectionDianService.selectFzInspectionDianListById(inspectionRouteInfoId);
            if (!CollectionUtils.isEmpty(fzInspectionDians)) {
                Map<String,FzInspectionDian> fzInspectionDianMap=fzInspectionDians.stream().collect(Collectors.toMap(FzInspectionDian::getInspectionId,FzInspectionDian->FzInspectionDian));
                for(FzInspectionRouteInfo fzInspectionRouteInfo1 : list){
                    FzInspectionRouteInfoVo fzInspectionRouteInfoVo=new FzInspectionRouteInfoVo();
                    BeanUtil.copyProperties(fzInspectionRouteInfo1,fzInspectionRouteInfoVo);
                    if(fzInspectionDianMap.containsKey(fzInspectionRouteInfoVo.getInspectionId())){
                        fzInspectionRouteInfoVo.setName(fzInspectionDianMap.get(fzInspectionRouteInfoVo.getInspectionId()).getName());
                        fzInspectionRouteInfoVos.add(fzInspectionRouteInfoVo);
                    }
                }
            }
        }
        return getDataTable(fzInspectionRouteInfoVos);
    }

    /**
     * 导出路线信息列表
     */
    @RequiresPermissions("inspection:info:export")
    @Log(title = "路线信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FzInspectionRouteInfo fzInspectionRouteInfo)
    {
        List<FzInspectionRouteInfo> list = fzInspectionRouteInfoService.selectFzInspectionRouteInfoList(fzInspectionRouteInfo);
        ExcelUtil<FzInspectionRouteInfo> util = new ExcelUtil<FzInspectionRouteInfo>(FzInspectionRouteInfo.class);
        return util.exportExcel(list, "info");
    }

    /**
     * 新增路线信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存路线信息
     */
    @RequiresPermissions("inspection:info:add")
    @Log(title = "路线信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FzInspectionRouteInfo fzInspectionRouteInfo)
    {
        return toAjax(fzInspectionRouteInfoService.insertFzInspectionRouteInfo(fzInspectionRouteInfo));
    }

    /**
     * 修改路线信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        FzInspectionRouteInfo fzInspectionRouteInfo = fzInspectionRouteInfoService.selectFzInspectionRouteInfoById(id);
        mmap.put("fzInspectionRouteInfo", fzInspectionRouteInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存路线信息
     */
    @RequiresPermissions("inspection:info:edit")
    @Log(title = "路线信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FzInspectionRouteInfo fzInspectionRouteInfo)
    {
        return toAjax(fzInspectionRouteInfoService.updateFzInspectionRouteInfo(fzInspectionRouteInfo));
    }

    /**
     * 删除路线信息
     */
    @RequiresPermissions("inspection:info:remove")
    @Log(title = "路线信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fzInspectionRouteInfoService.deleteFzInspectionRouteInfoByIds(ids));
    }

    @GetMapping("/dian")
    public String dian(ModelMap mmap,@RequestParam("routeId")String routeId)
    {
        mmap.put("routeId",routeId);
        return prefix + "/dian";
    }

    /**
     * 添加巡检点
     */
    @PostMapping( "/dianAdd")
    @ResponseBody
    public AjaxResult dianAdd(FzInspectionRouteInfo fzInspectionRouteInfo)
    {

    fzInspectionRouteInfo.setcId(GetSessionValueUtil.getCommunity());
    fzInspectionRouteInfo.setCreateBy(ShiroUtils.getSysUser().getUserName());
    return toAjax(fzInspectionRouteInfoService.insertFzInspectionRouteInfo(fzInspectionRouteInfo));
    }


    /**
     * 添加巡检点
     */
    @PostMapping( "/dianUpdate")
    @ResponseBody
    public AjaxResult dianUpdate(FzInspectionRouteInfo fzInspectionRouteInfo)
    {

        return toAjax(fzInspectionRouteInfoService.updateFzInspectionRouteInfo(fzInspectionRouteInfo));
    }

}
